﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Business;

namespace KhoBanHang
{
    public partial class DMNhanVien : Form
    {
        List<PhongBan> danhsachPhongban;
        List<NhanVien> danhsachNhanVien;

        public DMNhanVien()
        {
            InitializeComponent();
        }

        private void DMNhanVien_Load(object sender, EventArgs e)
        {
            LoadDanhSachPhongBan();
            LoadDanhSachNhanVien();

        }
        private void LoadDanhSachPhongBan() 
        {
            danhsachPhongban = Business.PhongBan.LayDanhSach();
            cbxPhongBan.DataSource = danhsachPhongban;

            cbxPhongBan.DisplayMember = "TenPB";;
            cbxPhongBan.ValueMember = "MaPB";

            cbxPhongBan.SelectedItem = null;    
           
        }

        private void LoadDanhSachNhanVien() 
        {
            
            danhsachNhanVien = Business.NhanVien.LayDanhSach();
            gridviewDanhSachNV.DataSource = danhsachNhanVien;

            gridviewDanhSachNV.Columns["MaPB"].Visible = false;

            gridviewDanhSachNV.Columns["TenDangNhap"].Visible = false;
            gridviewDanhSachNV.Columns["MatKhau"].Visible = false;
            gridviewDanhSachNV.Columns["Quyen"].Visible = false;

            gridviewDanhSachNV.Columns["NgaySinh"].DefaultCellStyle.Format = "dd/MM/yyyy";
            gridviewDanhSachNV.Columns["MaNVHienThi"].HeaderText = "Mã nhân viên";
            gridviewDanhSachNV.Columns["TenNV"].HeaderText = "Tên nhân viên";

            gridviewDanhSachNV.Columns["TenPB"].HeaderText = "Phòng ban";

            gridviewDanhSachNV.Columns["DiaChi"].HeaderText = "Địa chỉ";
            gridviewDanhSachNV.Columns["DienThoai"].HeaderText = "Điện thoại";

            gridviewDanhSachNV.Columns["NgaySinh"].HeaderText = "Ngày sinh";
            gridviewDanhSachNV.Columns["TheoDoi"].HeaderText = "Theo dõi";
            gridviewDanhSachNV.Columns["ChucVu"].HeaderText = "Chức vụ";
        }

        private void gridviewDanhSachNV_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            
            if (danhsachNhanVien != null) 
            {
                NhanVien nv = danhsachNhanVien[e.RowIndex];
                txtMaNV.Text = nv.MaNVHienThi;

                txtTenNV.Text = nv.TenNV;

                cbxPhongBan.SelectedValue = nv.MaPB;
                
                dtpickerNgaySinh.CustomFormat = "dd/MM/yyyy";
                dtpickerNgaySinh.Text = nv.NgaySinh.ToString();
                
                txtChucVu.Text = nv.ChucVu;

                txtDiaChi.Text = nv.DiaChi;
                txtDienThoai.Text = nv.DienThoai;
                if (nv.TheoDoi == true)
                    checkboxTheoDoi.Checked = true;
                else
                    checkboxTheoDoi.Checked = false;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
           
            txtMaNV.Text = "";
            txtTenNV.Text = "";
            dtpickerNgaySinh.Text = "";
            
            txtDiaChi.Text="";

            txtDienThoai.Text = "";

            txtChucVu.Text = "";
            cbxPhongBan.SelectedItem = null;
            checkboxTheoDoi.Checked = false;
            txtTenNV.Focus();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (KiemTra() == false)

                return;
            bool theodoi;

            if (checkboxTheoDoi.Checked == true)
                theodoi = true;
            else theodoi = false;
            if (txtMaNV.Text == "")
            {
                
                NhanVien.ThemMoi(new NhanVien(txtTenNV.Text, Int16.Parse(cbxPhongBan.SelectedValue.ToString()), txtChucVu.Text, txtDiaChi.Text,
                    txtDienThoai.Text, DateTime.Parse(dtpickerNgaySinh.Text), theodoi));

            }
            else
            {
                dtpickerNgaySinh.CustomFormat = "MM/dd/yyyy";

                NhanVien.CapNhat(new NhanVien( txtMaNV.Text,txtTenNV.Text, Int16.Parse(cbxPhongBan.SelectedValue.ToString()), txtChucVu.Text, txtDiaChi.Text,
                    txtDienThoai.Text, DateTime.Parse(dtpickerNgaySinh.Text), theodoi));

                dtpickerNgaySinh.CustomFormat = "dd/MM/yyyy";
            }



            LoadDanhSachNhanVien();
        }



        private bool KiemTra()
        {
            
            if(txtTenNV.Text==""||txtChucVu.Text==""||txtDiaChi.Text==""|| dtpickerNgaySinh.Text==""||                
                txtDienThoai.Text=="" || cbxPhongBan.SelectedValue==null)
            {
                MessageBox.Show("Nhập đầy đủ thông tin!");
                txtTenNV.Focus();

                return false;
            }
            return true;
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void txtDiaChi_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
